Imports System.Drawing

Partial Public Class Image
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim filename As String = "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\TenisLigaWeb\TenisLiga\Img\oldin.jpg"
        ImgOrigigi.ImageUrl = filename
        ImgThumb.ImageUrl = ResizeImage2(filename)
    End Sub

    Private Sub ResizeImage()

        Const Lx As Integer = 200  ' max width for thumbnails
        Const Ly As Integer = 240  ' max height for thumbnails
        Const upload_dir As String = "/upload_resize_test/"   ' directory to upload file
        Const upload_original As String = "sample"    ' filename to save original as (suffix added by script)
        Const upload_thumb As String = "thumb"    ' filename to save thumbnail as (suffix added by script)
        Const upload_max_size As Double = 25  ' max size of the upload (KB) note: this doesn't override any server upload limits
        Dim fileExt As String ' used to store the file extension (saves finding it mulitple times)
        Dim newWidth, newHeight As Double ' new width/height for the thumbnail
        Dim l2 As Integer ' temp variable used when calculating new size
        Dim fileFld As HttpPostedFile   ' used to grab the file upload from the form
        Dim originalimg As System.Drawing.Image ' used to hold the original image
        Dim msg As String ' display results
        Dim upload_ok As Boolean    ' did the upload work ?


        Dim filename As String = "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\TenisLigaWeb\TenisLiga\Img\oldin.jpg"
        Dim filenameThumb As String = "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\TenisLigaWeb\TenisLiga\Img\odlinThumb.jpg"
        originalimg = System.Drawing.Image.FromFile(filename)
        ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
        ' Note: if the original is smaller than the thumbnail size it will be scaled up
        If (originalimg.Width / Lx) > (originalimg.Width / Ly) Then
            l2 = originalimg.Width
            newWidth = Lx
            newHeight = originalimg.Height * (Lx / l2)
            If newHeight > Ly Then
                newWidth = newWidth * (Ly / newHeight)
                newHeight = Ly
            End If
        Else
            l2 = originalimg.Height
            newHeight = Ly
            newWidth = originalimg.Width * (Ly / l2)
            If newWidth > Lx Then
                newHeight = newHeight * (Lx / newWidth)
                newWidth = Lx
            End If
        End If

        Dim thumb As New Bitmap(Convert.ToInt32(newWidth), Convert.ToInt32(newHeight))

        'Create a graphics object           
        Dim gr_dest As Graphics = Graphics.FromImage(thumb)

        ' just in case it's a transparent GIF force the bg to white 
        Dim sb As SolidBrush = New SolidBrush(System.Drawing.Color.White)
        gr_dest.FillRectangle(sb, 0, 0, Convert.ToInt32(thumb.Width), Convert.ToInt32(thumb.Height))

        'Re-draw the image to the specified height and width
        gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)

        Try
            'fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
            fileExt = System.IO.Path.GetExtension(filename).ToLower()
            'originalimg.Save(Server.MapPath(upload_dir & upload_original & fileExt), originalimg.RawFormat)
            thumb.Save(filenameThumb, originalimg.RawFormat)
            'msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & upload_original & fileExt)
            upload_ok = True
        Catch ex As Exception
            msg = "Sorry, there was a problem saving the image.<br>"
            msg = msg + ex.Message
        End Try
        ' Housekeeping for the generated thumbnail
        If Not thumb Is Nothing Then
            thumb.Dispose()
            thumb = Nothing
        End If
    End Sub

    Private Function ResizeImage2(ByVal filename As String) As String
        Const Lx As Integer = 200  ' max width for thumbnails
        Const Ly As Integer = 240  ' max height for thumbnails
        Dim upload_dir As String = Server.MapPath("") + "img\"
        Const upload_original As String = "sample"    ' filename to save original as (suffix added by script)
        Const upload_thumb As String = "thumb"    ' filename to save thumbnail as (suffix added by script)
        Const upload_max_size As Double = 25  ' max size of the upload (KB) note: this doesn't override any server upload limits
        Dim fileExt As String ' used to store the file extension (saves finding it mulitple times)
        Dim newWidth, newHeight As Double ' new width/height for the thumbnail
        Dim l2 As Integer ' temp variable used when calculating new size
        Dim fileFld As HttpPostedFile   ' used to grab the file upload from the form
        Dim originalimg As System.Drawing.Image ' used to hold the original image
        Dim msg As String ' display results
        Dim upload_ok As Boolean    ' did the upload work ?


        fileExt = System.IO.Path.GetExtension(filename).ToLower()
        Dim filenameThumb As String = filename.Replace(fileExt, "Thumb" + fileExt)
        originalimg = System.Drawing.Image.FromFile(filename)
        ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
        ' Note: if the original is smaller than the thumbnail size it will be scaled up
        If (originalimg.Width / Lx) > (originalimg.Width / Ly) Then
            l2 = originalimg.Width
            newWidth = Lx
            newHeight = originalimg.Height * (Lx / l2)
            If newHeight > Ly Then
                newWidth = newWidth * (Ly / newHeight)
                newHeight = Ly
            End If
        Else
            l2 = originalimg.Height
            newHeight = Ly
            newWidth = originalimg.Width * (Ly / l2)
            If newWidth > Lx Then
                newHeight = newHeight * (Lx / newWidth)
                newWidth = Lx
            End If
        End If

        Dim thumb As New Bitmap(Convert.ToInt32(newWidth), Convert.ToInt32(newHeight))

        'Create a graphics object           
        Dim gr_dest As Graphics = Graphics.FromImage(thumb)

        ' just in case it's a transparent GIF force the bg to white 
        Dim sb As SolidBrush = New SolidBrush(System.Drawing.Color.White)
        gr_dest.FillRectangle(sb, 0, 0, Convert.ToInt32(thumb.Width), Convert.ToInt32(thumb.Height))

        'Re-draw the image to the specified height and width
        gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)

        Try
            'fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()

            'originalimg.Save(Server.MapPath(upload_dir & upload_original & fileExt), originalimg.RawFormat)
            thumb.Save(filenameThumb, originalimg.RawFormat)
            'msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & upload_original & fileExt)
            upload_ok = True
        Catch ex As Exception
            msg = "Sorry, there was a problem saving the image.<br>"
            msg = msg + ex.Message
        End Try
        ' Housekeeping for the generated thumbnail
        If Not thumb Is Nothing Then
            thumb.Dispose()
            thumb = Nothing
        End If
        Return filenameThumb
    End Function


End Class